package org.jiucai.app;

import java.util.Locale;
import java.util.Map;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.codehaus.jackson.map.SerializationConfig;
import org.jiucai.appframework.base.spring.service.BaseService;
import org.jiucai.appframework.base.spring.web.render.JsonRender;
import org.jiucai.appframework.base.spring.web.render.XmlRender;
import org.jiucai.appframework.base.util.ConfigUtil;
import org.jiucai.appframework.common.util.BaseUtil;
import org.jiucai.appframework.common.util.LogUtil;
import org.jiucai.appframework.common.util.Logs;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 所有服务类的基类
 * 
 * @author zhaidw
 * 
 */
public abstract class AppBaseService extends BaseService {

	protected static Logs logger = LogUtil.getLog(AppBaseService.class);
	
	protected static FastDateFormat dateFormater = FastDateFormat.getInstance(
			"yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE);

	/**
	 * xml 或 json 响应的默认编码
	 */
	protected static String encoding = "UTF-8";

	/**
	 * XML 字符串生成器
	 */
	@Autowired
	private XmlRender xmlRender;

	/**
	 * json 字符串生成器
	 */
	@Autowired
	private JsonRender jsonRender;
	
	
	
	/**
	 * 配置项读取类
	 */
	protected static Configuration config;


	public static Configuration getConfig() {
		return config;
	}

	static {
		config = ConfigUtil.addConfig("config");
	}
	

	/**
	 * 返回转换 xml 的工具对象
	 * 
	 * @return
	 */
	public XmlRender getXmlRender() {
		xmlRender.setEncoding(encoding);
		return xmlRender;
	}

	/**
	 * 返回转换 json 工具对象
	 * 
	 * @return
	 */
	public JsonRender getJsonRender() {
		jsonRender.setEncoding(encoding);
		jsonRender.getObjectMapper().configure(
				SerializationConfig.Feature.INDENT_OUTPUT, false);
		return jsonRender;
	}

	/**
	 * 返回 json 格式的成功或错误信息
	 * 
	 * @param isSuccess
	 *            true 表示成功消息 false 表示错误消息
	 * @param msg
	 * @return
	 */
	protected String getJsonMsg(Boolean isSuccess, String msg) {
		StringBuffer result = new StringBuffer("");

		if (isSuccess) {
			if (StringUtils.isBlank(msg)) {
				msg = "ok";
			}
			result.append("{\"success\":\"").append(msg).append("\"}");
		} else {
			if (StringUtils.isBlank(msg)) {
				msg = "failed";
			}
			result.append("{\"error\":\"").append(msg).append("\"}");
		}

		return result.toString();
	}

	/**
	 * 返回 chartXML 格式的成功或错误信息
	 * 
	 * @param isSuccess
	 *            true 表示成功消息 false 表示错误消息
	 * @param msg
	 * @return
	 */
	protected String getXmlMsg(Boolean isSuccess, String msg) {

		StringBuffer result = new StringBuffer("");

		if (isSuccess) {
			if (StringUtils.isBlank(msg)) {
				msg = "ok";
			}
			result.append("<chart><success>").append(msg)
					.append("</success></chart>");
		} else {
			if (StringUtils.isBlank(msg)) {
				msg = "failed";
			}
			result.append("<chart><error>").append(msg)
					.append("</error></chart>");
		}

		return result.toString();
	}

	protected String convertMapValue(Object val) {
		String result = BaseUtil.convertMapValue(val);

		// 把计算处理中分母为0的结果都替换为 -
		if (String.valueOf(AppBaseDao.ZERO_DIVISOR).equals(result)) {
			return "-";
		}

		return result;
	}

	/**
	 * 转换并格式化 map 中的值（空值处理，百分比，小数位数格式化）
	 * 
	 * @param valueMap
	 */
	public void convertMap(Map<String, Object> valueMap) {

		if (valueMap != null) {
			for (String key : valueMap.keySet()) {
				if (valueMap.get(key) != null) {
					Object val = valueMap.get(key);
					// 格式化数据格式：将计算处理中分母为0的结果都替换为 -
					String reVal = convertMapValue(val);

					valueMap.put(key, reVal);
				} else {
					valueMap.put(key, "-");
				}
			}

		}
	}

}
